package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.pojo.Looper;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.ILooperService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@PreAuthorize("@permission.admin()")
@RestController
@RequestMapping("/admin/looper")
public class LooperAdminApi {

    final
    ILooperService looperService;

    public LooperAdminApi(ILooperService looperService) {
        this.looperService = looperService;
    }

    /**
     * 添加轮播图
     *
     * @param _looper 轮播图
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.looper_title_cant_empty_code, message = ResponseState.looper_title_cant_empty_message),
            @ApiResponse(code = ResponseState.looper_image_url_cant_empty_code, message = ResponseState.looper_image_url_cant_empty_message),
            @ApiResponse(code = ResponseState.looper_target_url_cant_empty_code, message = ResponseState.looper_title_cant_empty_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PostMapping
    public ResponseResult insertLoop(@RequestBody Looper _looper) {
        return looperService.insert(_looper);
    }

    /**
     * 删除 looper 对象
     *
     * @param _Id 唯一id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") String _Id) {
        return looperService.delete(_Id);
    }

    /**
     * 查询looper 对象
     *
     * @param _Id 唯一id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/{id}")
    public ResponseResult query(@PathVariable("id") String _Id) {
        return looperService.query(_Id);
    }

    /**
     * 更新 looper 对象
     *
     * @param _Id 唯一id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PutMapping("/{id}")
    public ResponseResult update(@PathVariable("id") String _Id,
                                 @RequestBody Looper _looper) {
        return looperService.update(_Id, _looper);
    }

    /**
     * 获取looper 列表
     *
     * @param page 页码
     * @param size 页面大小
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/list")
    public ResponseResult list(@RequestParam("page") int page, @RequestParam("size") int size) {
        return looperService.listForAdmin();
    }
}
